//
//  ZiLiaoTableViewCell.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/3.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "ZiLiaoTableViewCell.h"
#import "IGFocusmember.h"
#import "UIView+Frame.h"

@interface ZiLiaoTableViewCell () {
    UIImageView *icon;
    UILabel *name;
    
    
    void(^blocks)(IGFocusmember *info);
}

@end

@implementation ZiLiaoTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _initViews];
    }
    return self;
}

- (void)_initViews {
    icon = [[UIImageView alloc] initWithFrame:CGRectZero];
    icon.contentMode = UIViewContentModeScaleAspectFill;
    icon.layer.masksToBounds = YES;
    [self.contentView addSubview:icon];
    
    name = [[UILabel alloc] initWithFrame:CGRectZero];
    name.font = [UIFont systemFontOfSize:17];
    [self.contentView addSubview:name];
    
    self.follow = [UIButton buttonWithType:UIButtonTypeCustom];
    _follow.layer.cornerRadius = 5;
    _follow.layer.borderWidth = 1;
    _follow.layer.borderColor=[UIColor lightGrayColor].CGColor;
    _follow.titleLabel.font = [UIFont systemFontOfSize:16];
    [_follow addTarget:self action:@selector(follAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_follow];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    IGFocusmember *info = self.data;
    
    
    icon.frame = CGRectMake(15, 15, self.height-30, self.height-30);
    icon.layer.cornerRadius = icon.width/2;
    [icon sd_setImageWithURL:[NSURL URLWithString:info.iconImage] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    
//    name.text = info.otherNike.length?info.otherNike:@"无昵称";
    name.text = info.otherNike.length?info.otherNike:NSLocalizedString(@"No Nickname", nil);
    [name sizeToFit];
    name.x = icon.right+15;
    name.centerY = self.height/2;
    
    _follow.frame = CGRectMake(0, 0, 70, 30);
    _follow.right = self.width-15;
    _follow.centerY = self.height/2;
    if ([info.attentionState integerValue]==0) { //是否 关注  0=已关注，1=未关注
        _follow.layer.borderColor = [UIColor clearColor].CGColor;
//        [_follow setTitle:@"取消关注" forState:UIControlStateNormal];
        [_follow setTitle:NSLocalizedString(@"Unfollow", nil) forState:UIControlStateNormal];
        [_follow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    } else {
        _follow.layer.borderColor = [UIColor clearColor].CGColor;
//        [_follow setTitle:@"＋ 关注" forState:UIControlStateNormal];
        [_follow setTitle:[NSString stringWithFormat:@"+ %@",NSLocalizedString(@"Follow", nil)] forState:UIControlStateNormal];
        [_follow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    if (name.right > _follow.left)
    {
        name.width = _follow.left - 5 -name.left;
    }
}

- (void)follAction:(UIButton *)btn
{
    if (blocks)
    {
        blocks((IGFocusmember *)self.data);
    }
}

- (void)followOwerAction:(void(^)(IGFocusmember *info))block
{
    blocks = [block copy];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end
